package internal

import (
    "log"
    "net/http"
    "strconv"
)

type Probe struct {
    workers int
    url     string
}

func NewProbe(workers int, url string) *Probe {
    return &Probe{workers: workers, url: url}
}

func (p *Probe) GetFileSize() int {
    size := -1
    client := &http.Client{}
    request, err := http.NewRequest("HEAD", p.url, nil)
    if err != nil {
        log.Panicln(err)
    }

    resp, err := client.Do(request)
    if err != nil {
        log.Fatalln(err)
    }

    if contentLength, ok := resp.Header["Content-Length"]; ok {
        fileSize, err := strconv.Atoi(contentLength[0])
        if err != nil {
            log.Fatalln("File size could not be determined:", err)
        }
        size = fileSize / p.workers
    } else {
        log.Fatalln("File size was not provided!")
    }
    return size
}
